package com.atguigu.gmall.mq.config;

import com.sun.jndi.ldap.LdapURL;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class DeadLetterMqConfig {

    // 声明一些变量
    public static final String exchange_dead = "exchange.dead";
    public static final String routing_dead_1 = "routing.dead.1";
    public static final String routing_dead_2 = "routing.dead.2";
    public static final String queue_dead_1 = "queue.dead.1";
    public static final String queue_dead_2 = "queue.dead.2";
   //创建死信交换机

    @Bean
    public DirectExchange exchange(){
        //返回对象！
        return new DirectExchange(exchange_dead,true,false);
    }

    //创建队列1
    @Bean
    // 创建一个map来设置消息的存活时间
    public Queue queue1(){
        HashMap<String, Object> map = new HashMap<>();
        //设置过期时间10秒  [1、消息拒收 2、消息过期 3、消息溢出 都走路由key2]
        map.put("x-message-ttl",10000);
        map.put("x-dead-letter-exchange",exchange_dead);
        map.put("x-dead-letter-routing-key",routing_dead_2);
        return new Queue(queue_dead_1,true,false,false,map);
    }
    @Bean
    public Binding binding1(){
        // 将队列一 通过routing_dead_1 key 绑定到exchange_dead 交换机上
        return BindingBuilder.bind(queue1()).to(exchange()).with(routing_dead_1);

    }
    //创建队列2
    @Bean
    //这个队列二就是一个普通队列
    public Queue queue2(){
        return new Queue(queue_dead_2,true,false,false,null);
    }
    @Bean
    public Binding binding2(){
        // 将队列二通过routing_dead_2 key 绑定到exchange_dead交换机上！
        return BindingBuilder.bind(queue2()).to(exchange()).with(routing_dead_2);
    }
}
